<?php


namespace app\home\controller;

use app\base\BaseController;
use think\facade\Db;

/**
 * Class CostAccount
 * @package app\home\controller
 * 成本核算
 */
class CostAccount extends BaseController
{
    protected $table='ruku_view';
    protected $title='成本核算';
    private $sessionKey='pageCostAccount';
    public function index()
    {
        $param=$this->param;
        if (request()->isAjax()) {

            $where[]=['yw_type','=','生产入库'];
            $where[]=['status','=','2'];
            foreach (['bar_code','mdName','specs'] as $i){
                if (isset($param[$i])&&$param[$i]){
                    $where[]=[$i,'like','%'.$param[$i].'%'];
                }
            }
            session($this->sessionKey,$where);
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->where($where)
                ->field("*,DATE_FORMAT(doDate, '%Y-%m') as doDate")
                ->group('doDate,material_dossier_id')
                ->paginate($rows)->each(function ($item){
                    $mdId=$item['material_dossier_id'];
                    $mon=$item['doDate'];
                    $item['clcb']=0;
                    $item['rgcb']=0;
                    $item['ftcb']=0;
                    $item['cbhj']=0;
                    $item['cbdj']=0;
                    $inNum=Db::name($this->table)
                        ->where('status','2')
                        ->where('doDate','like',$item['doDate'].'%')
                        ->where('material_dossier_id',$item['material_dossier_id'])->sum('inNum');
                    $item['inNum']=$inNum;
                    $plan_id=Db::name('production_plan_view')->where('status',3)
                        ->where('material_dossier_id',$mdId)
                        ->where('planned_time','like',$mon.'%')
                        ->column('production_plan_id');
                    $clcb=0;
                    $rgcb=0;
                    if ($plan_id){
                        $ycl=Db::name('production_plan_ycl')->where('status','>',-1)
                            ->where('production_plan_id','in',$plan_id)
                            ->field('material_dossier_id,ckNum')->select();
                        $aa=[];
                        foreach ($ycl as $yc){
                            $idkey='id_'.$yc['material_dossier_id'];
                            if (isset($aa[$idkey])){
                                $aa[$idkey]+=$yc['ckNum'];
                            }else{
                                $aa[$idkey]=$yc['ckNum'];
                            }
                        }

                        //材料成本
                        foreach ($aa as $ke=>$v){
                            $idk=explode('_',$ke);
                            $dj=Db::name('purveyor_price')->where('time',$mon)
                                ->where('p_type',1)->where('material_dossier_id',$idk[1])->value('price');
                            $dj=$dj??0;
                            $clcb+=$dj*$v;
                        }
                        //人工成本
                        $rgcb=Db::name("piece_rate_detail")->where('status',2)
                            ->where('plan_id','in',$plan_id)->sum('salary');
                    }
                    $item['clcb']=$clcb;
                    $item['rgcb']=$rgcb;

                    $ftList=Db::name('cost_share_data_detail')->alias('csdd')
                        ->join('cost_share cs','cs.id=csdd.cost_share_id')
                        ->where('csdd.status',2)
                        ->where('csdd.material_dossier_id',$item['material_dossier_id'])
                        ->where('cs.sharStart','>=',date('Y-m-01',strtotime($mon)))
                        ->where('cs.sharStart','<=',date('Y-m-t',strtotime($mon)))
                        ->field('cs.sharType,gxPrice,costPrice')->select();
                    $slDj=0;
                    $gxDj=0;
//                    halt(Db::name('cost_share_data_detail')->getLastSql());
                    foreach ($ftList as $ft){
                        if ($ft['sharType']==1){
                            $slDj+=$ft['costPrice'];
                        }else{
                            $gxDj+=$ft['gxPrice'];
                        }
                    }
                    $ftcb=$slDj+($gxDj*$inNum);
                    $item['ftcb']=round($ftcb,2);
                    $cbhj=$clcb+$rgcb+$ftcb;
                    $item['cbhj']=$cbhj;

                    $item['cbdj']=round($cbhj/$inNum,2);
                    return $item;
                });

            return table_assign(0, '', $list);
        } else {
            return view();
        }
 }

    public function exportData()
    {
        $where=session($this->sessionKey);

        $list = Db::name($this->table)
            ->where($where)
            ->field("*,DATE_FORMAT(doDate, '%Y-%m') as doDate")
            ->group('doDate,material_dossier_id')
            ->select()
            ->each(function ($item){

                $mdId=$item['material_dossier_id'];
                $mon=$item['doDate'];
                $item['clcb']=0;
                $item['rgcb']=0;
                $item['ftcb']=0;
                $item['cbhj']=0;
                $item['cbdj']=0;
                $inNum=Db::name($this->table)
                    ->where('status','2')
                    ->where('doDate','like',$item['doDate'].'%')
                    ->where('material_dossier_id',$item['material_dossier_id'])->sum('inNum');
                $item['inNum']=$inNum;
                $plan_id=Db::name('production_plan_view')->where('status',3)
                    ->where('material_dossier_id',$mdId)
                    ->where('planned_time','like',$mon.'%')
                    ->column('production_plan_id');
                $clcb=0;
                $rgcb=0;
                if ($plan_id){
                    $ycl=Db::name('production_plan_ycl')->where('status','>',-1)
                        ->where('production_plan_id','in',$plan_id)
                        ->field('material_dossier_id,ckNum')->select();
                    $aa=[];
                    foreach ($ycl as $yc){
                        $idkey='id_'.$yc['material_dossier_id'];
                        if (isset($aa[$idkey])){
                            $aa[$idkey]+=$yc['ckNum'];
                        }else{
                            $aa[$idkey]=$yc['ckNum'];
                        }
                    }

                    //材料成本
                    foreach ($aa as $ke=>$v){
                        $idk=explode('_',$ke);
                        $dj=Db::name('purveyor_price')->where('time',$mon)
                            ->where('p_type',1)->where('material_dossier_id',$idk[1])->value('price');
                        $dj=$dj??0;
                        $clcb+=$dj*$v;
                    }
                    //人工成本
                    $rgcb=Db::name("piece_rate_detail")->where('status',2)
                        ->where('plan_id','in',$plan_id)->sum('salary');
                }
                $item['clcb']=$clcb;
                $item['rgcb']=$rgcb;

                $ftList=Db::name('cost_share_data_detail')->alias('csdd')
                    ->join('cost_share cs','cs.id=csdd.cost_share_id')
                    ->where('csdd.status',2)
                    ->where('csdd.material_dossier_id',$item['material_dossier_id'])
                    ->where('cs.sharStart','>=',date('Y-m-01',strtotime($mon)))
                    ->where('cs.sharStart','<=',date('Y-m-t',strtotime($mon)))
                    ->field('cs.sharType,gxPrice,costPrice')->select();
                $slDj=0;
                $gxDj=0;
//                    halt(Db::name('cost_share_data_detail')->getLastSql());
                foreach ($ftList as $ft){
                    if ($ft['sharType']==1){
                        $slDj+=$ft['costPrice'];
                    }else{
                        $gxDj+=$ft['gxPrice'];
                    }
                }
                $ftcb=$slDj+($gxDj*$inNum);
                $item['ftcb']=round($ftcb,2);
                $cbhj=$clcb+$rgcb+$ftcb;
                $item['cbhj']=$cbhj;

                $item['cbdj']=round($cbhj/$inNum,2);
                return $item;
            });
        $title = [
//            'id' => 'ID',
            'doDate' => '月份',
            'bar_code' => '产品编码',
            'mdName' => '产品名称',
            'specs' => '规格型号',
            'unit' => '单位',
            'clcb' => '材料成本',
            'rgcb' => '人工成本',
            'ftcb' => '分摊成本',
            'cbhj' => '成本合计',
            'inNum' => '入库数量',
            'cbdj' => '成本单价',
        ];
        $name =$this->title;
        exportXlsData($name,$title,$list);
        exit();
    }
}